<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/22
 * Time: 9:34
 */

namespace app\models;

use yii;
use yii\db\Query;

class QuoteProduct extends BaseModel
{
    public function __construct()
    {
        $this->table = yii::$app->db->tablePrefix . 'quote_product';
    }

    public function all($where = [], $offset = false, $limit = false, $order = false)
    {
        $db = new Query();
        $db->select('*')->from($this->table);
        if (!empty($where['date'])) {
            $db->andWhere(['>=', 'date', $where['date']]);
        }
        if (!empty($where['enddate'])) {
            $db->andWhere(['<=', 'date', $where['enddate']]);
        }
        if (!empty($where['job_number'])) {
            $db->andWhere(['job_number' => $where['job_number']]);
        }

        if (!empty($where['product_name'])) {
            $db->andWhere(['like', 'product_name', $where['product_name']]);
        }
        if (!empty($where['id'])) {
            if (is_array($where['id'])) {
                $db->andWhere(['in', 'id', $where['id']]);
            } else {
                $db->andWhere(['id' => $where['id']]);
            }
        }

        $db->andWhere(['!=', 'img_src', '']);
        if ($order) {
            $db->orderBy($order);
        }
        if ($offset) {
            $db->offset($offset);
        }
        if ($limit) {
            $db->limit($limit);
        }
        return $db->all();
    }

    public function one($where = [])
    {

    }

    public function size($where = [])
    {
        $db = new Query();
        $db->select('count(id) as num')->from($this->table);
        if (!empty($where['date'])) {
            $db->andWhere(['>=', 'date', $where['date']]);
        }
        if (!empty($where['enddate'])) {
            $db->andWhere(['<=', 'date', $where['enddate']]);
        }
        if (!empty($where['job_number'])) {
            $db->andWhere(['job_number' => $where['job_number']]);
        }

        if (!empty($where['product_name'])) {
            $db->andWhere(['like', 'product_name', $where['product_name']]);
        }
        if (!empty($where['id'])) {
            if (is_array($where['id'])) {
                $db->andWhere(['in', 'id', $where['id']]);
            } else {
                $db->andWhere(['id' => $where['id']]);
            }
        }

        $db->andWhere(['!=', 'img_src', '']);
        $info = $db->one();
        return $info ? $info['num'] : 0;
    }

}